﻿using BLL.Entities;
using BLL.Repositories;
using GLB.Global;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using AutoMapper;
using VM = _17bangMVC.Models;
using SRV.ViewModel.Article;
using SRV.ViewModel.Email;

namespace SRV.ProdService
{
    public class BaseService
    {
        private UserRepository userRepository;
        protected static readonly MapperConfiguration config;
        static BaseService()
        {
            config = new MapperConfiguration
                (cfg =>
                {
                    cfg.CreateMap<Article, SingleModel>();
                    cfg.CreateMap<VM.Article.NewModel, Article>();
                    cfg.CreateMap<VM.UserModel, User>().ReverseMap();
                    cfg.CreateMap<Article, EditModel>().ReverseMap();
                    cfg.CreateMap<BLL.Entities.Profile, VM.ProfileModel>();
                    cfg.CreateMap<User, ActivateModel>();
                });
        }
        public BaseService()
        {
            userRepository = new UserRepository(context);
        }
        protected IMapper mapper
        {
            get { return config.CreateMapper(); }
        }

        protected SqlDbContext context
        {
            get
            {
                if (HttpContext.Current.Items[Keys.DbContext] == null)
                {
                    SqlDbContext sqlDb = new SqlDbContext();
                    sqlDb.Database.BeginTransaction();
                    HttpContext.Current.Items[Keys.DbContext] = sqlDb;
                }
                return (SqlDbContext)HttpContext.Current.Items[Keys.DbContext];
            }
        }
        private static SqlDbContext getContextFromHttp()
        {
            SqlDbContext objContext = HttpContext.Current.Items[Keys.DbContext] as SqlDbContext;
            HttpContext.Current.Items.Remove(Keys.DbContext);
            return objContext;
        }
        public static void Commit()
        {
            SqlDbContext context = getContextFromHttp();
            if (context != null)
            {
                context.SaveChanges();
                using (context)
                {
                    using (DbContextTransaction transaction = context.Database.CurrentTransaction)
                    {
                        transaction.Commit();
                    }
                }
            }
        }
        public static void Rollback()
        {
            SqlDbContext context = getContextFromHttp();
            if (context != null)
            {
                using (context)
                {
                    using (DbContextTransaction transaction = context.Database.CurrentTransaction)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
        public User GetCurrentUser()
        {
            //取出cookie的信息
            NameValueCollection userInfo = HttpContext.Current.Request.Cookies[Keys.User].Values;
            if (userInfo == null)
            {
                return null;
            }
            //取出cookie的id
            bool hasValue = int.TryParse
                (userInfo[Keys.Id], out int currentUserId);
            if (!hasValue)
            {
                //如果cookie不存在，还要继续执行代码，把cookie删掉
                throw new ArgumentException("cookie的当前用户的Id不能为空");
            }
            string pwdInCookie = userInfo[Keys.Password];
            if (pwdInCookie == null)
            {
                throw new ArgumentException("cookie的当前用户的password不能为空");
            }
            User current = userRepository.Find(currentUserId);
            if (current.Password != pwdInCookie)
            {
                throw new ArgumentException("cookie中的密码和输入密码不一致");
            }
            return current;
        }
    }
}
